package es.bwain.xprt.preproduction;

import java.util.ArrayList;
import java.util.List;

public class HtmlEntitiesGenerator {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		HtmlEntity text = new HtmlEntity("text", "input");

		text.addAttribute("accesskey");
		text.addAttribute("alt");
		text.addAttribute("class");
		text.addAttribute("disabled");
		text.addAttribute("id");
		text.addAttribute("maxlength");
		text.addAttribute("name", true, true);
		text.addAttribute("onblur");
		text.addAttribute("onchange");
		text.addAttribute("onclick");
		text.addAttribute("ondblclick");
		text.addAttribute("onfocus");
		text.addAttribute("onhelp");
		text.addAttribute("onkeydown");
		text.addAttribute("onkeypress");
		text.addAttribute("onkeyup");
		text.addAttribute("onmousedown");
		text.addAttribute("onmousemove");
		text.addAttribute("onmouseout");
		text.addAttribute("onmouseover");
		text.addAttribute("onmouseup");
		text.addAttribute("onselect");
		text.addAttribute("readonly");
		text.addAttribute("size");
		text.addAttribute("style");
		text.addAttribute("tabindex");
		text.addAttribute("title");
		text.addAttribute("type", false, false, null, "text");
		text.addAttribute("value", false, false, null, "/root/form/*[name()=$name]", true);
		
		HtmlEntity form = new HtmlEntity("form", "form");
		form.addAttribute("action", true, true, null, null, true, "concat($contextPath,", ",$servletMapping)");
		form.addAttribute("accept-charset");
		form.addAttribute("class");
		form.addAttribute("enctype");
		form.addAttribute("id");
		form.addAttribute("method", true, false, "post");
		form.addAttribute("name");
		form.addAttribute("onreset");
		form.addAttribute("onsubmit");
		form.addAttribute("style");
		form.addAttribute("text", true, true);
		
		HtmlEntity link = new HtmlEntity("link", "a");
		link.addAttribute("accesskey");
		link.addAttribute("anchor");
		link.addAttribute("class");
		link.addAttribute("href", true, true, null, null, true, "concat($contextPath,", ",$servletMapping,$anchor,'?',$query-string)");
		link.addAttribute("id");
		link.addAttribute("name");
		link.addAttribute("onblur");
		link.addAttribute("onclick");
		link.addAttribute("ondblclick");
		link.addAttribute("onfocus");
		link.addAttribute("onhelp");
		link.addAttribute("onkeydown");
		link.addAttribute("onkeypress");
		link.addAttribute("onkeyup");
		link.addAttribute("onmousedown");
		link.addAttribute("onmousemove");
		link.addAttribute("onmouseout");
		link.addAttribute("onmouseover");
		link.addAttribute("onmouseup");
		link.addAttribute("query-string");
		link.addAttribute("style");
		link.addAttribute("tabindex");
		link.addAttribute("target");
		link.addAttribute("title");
		link.addAttribute("text", true, true);
		
		HtmlEntity select = new HtmlEntity("select", "select");
		select.addAttribute("class");
		select.addAttribute("disabled");
		select.addAttribute("id");
		select.addAttribute("multiple");
		select.addAttribute("name", true, true);
		select.addAttribute("onblur");
		select.addAttribute("onchange");
		select.addAttribute("onclick");
		select.addAttribute("ondblclick");
		select.addAttribute("onfocus");
		select.addAttribute("onhelp");
		select.addAttribute("onkeydown");
		select.addAttribute("onkeypress");
		select.addAttribute("onkeyup");
		select.addAttribute("onmousedown");
		select.addAttribute("onmousemove");
		select.addAttribute("onmouseout");
		select.addAttribute("onmouseover");
		select.addAttribute("onmouseup");
		select.addAttribute("size");
		select.addAttribute("style");
		select.addAttribute("tabindex");
		select.addAttribute("title");
		select.addAttribute("text", true, true);
		
		HtmlEntity option = new HtmlEntity("option", "option");
		option.addAttribute("class");
		option.addAttribute("style");
		option.addAttribute("selected");
		option.addAttribute("value");
		option.addAttribute("text");
		
		List<HtmlEntity> entities = new ArrayList<HtmlEntity>();
		entities.add(text);
		entities.add(form);
		entities.add(link);
		entities.add(select);
		entities.add(option);
		
		
		StringBufferPlus sb = new StringBufferPlus();
		
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		sb.append("<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">");
		sb.sp();
		
		for (HtmlEntity entity : entities) {
			sb.append("\t<xsl:template name=\"" + entity.getName() + "\">");
			for (HtmlAttribute attr : entity.getAttributes()) {
				if (attr.isPassthrough()) {
					if (attr.isRequired()) {
						sb.append("\t\t<xsl:param name=\"" + attr.getName() + "\"/>");
					} else {
						sb.append("\t\t<xsl:param name=\"" + attr.getName() + "\">" + ((attr.getDefaultValue()!=null)?attr.getDefaultValue():" ") + "</xsl:param>");
					}
				}
			}
			sb.append("\t\t<xsl:call-template name=\"html-element\">");
			sb.append("\t\t\t<xsl:with-param name=\"element\">" + entity.getEntity() + "</xsl:with-param>");
			for (HtmlAttribute attr : entity.getAttributes()) {
				if (attr.getFixedValue()!=null) {
					if (attr.isXpathValue()) {
						sb.append("\t\t\t<xsl:with-param name=\"" + attr.getName() + "\" select=\"" + attr.getFixedValue() + "\"/>");
					} else {
						sb.append("\t\t\t<xsl:with-param name=\"" + attr.getName() + "\">" + attr.getFixedValue() + "</xsl:with-param>");
					}
				} else {
					String before = "$", after = "";
					if (attr.getConcatBefore() != null) {
						before = attr.getConcatBefore() + "$";
					}
					if (attr.getConcatAfter() != null) {
						after = attr.getConcatAfter();
					}
					sb.append("\t\t\t<xsl:with-param name=\"" + attr.getName() + "\" select=\"" + before + attr.getName() + after + "\"/>");
				}
			}
			sb.append("\t\t</xsl:call-template>");
			sb.append("\t</xsl:template>");
			sb.sp();
		}

		sb.append("\t<xsl:template name=\"html-element\">");
		sb.append("\t\t<xsl:param name=\"element\"/>");
		for (String attr : HtmlTemplate.attributes.keySet()) {
			sb.append("\t\t<xsl:param name=\"" + attr + "\"> </xsl:param>");
		}
		sb.append("\t\t<xsl:element name=\"{$element}\">");
		for (String attr : HtmlTemplate.attributes.keySet()) {
			if (!attr.equals("text")) {
				sb.append("\t\t\t<xsl:if test=\"$" + attr + "!=''\">");
				sb.append("\t\t\t\t<xsl:attribute name=\"" + attr + "\"><xsl:value-of select=\"$" + attr + "\"/></xsl:attribute>");
				sb.append("\t\t\t</xsl:if>");
			}
		}
		if (HtmlTemplate.attributes.containsKey("text")) {
			sb.append("\t\t\t<xsl:copy-of select=\"$text\"/>");
		}
		sb.append("\t\t</xsl:element>");
		sb.append("\t</xsl:template>");
		
		sb.sp();
		
		sb.append("</xsl:stylesheet>");
		System.out.println(sb.toString());
	}

}
